package com.niiwoo.civet.mobile.lanmao.handler;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.niiwoo.civet.account.dto.request.UpdateBankCardMobileNoRequestDTO;
import com.niiwoo.civet.account.service.bankcard.BankCardDubboService;
import com.niiwoo.tripod.lanmao.enums.BankCodeEnum;
import com.niiwoo.tripod.lanmao.enums.LanMaoGatewayServiceNameEnum;
import com.niiwoo.tripod.provider.exception.BizException;
import java.util.Collections;
import lombok.Getter;
import lombok.Setter;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

@Component
public class BankCardMobileNoNotifyHandler implements LanMaoNotifyHandler {

    public static final Logger logger = LoggerFactory.getLogger(BankCardMobileNoNotifyHandler.class);

    @Reference(version = "1.0.0")
    BankCardDubboService bankCardDubboService;

    @Override
    public boolean supports(String serviceName) {
        return LanMaoGatewayServiceNameEnum.MODIFY_MOBILE_EXPAND.name().equals(serviceName);
    }

    @Override
    public HandleResult handle(JSONObject json) {
        logger.info("修改银行卡预留手机号回调处理###报文###data={}", json.toJSONString());
        BankCardMobileNoGatewayResponse response = json.toJavaObject(BankCardMobileNoGatewayResponse.class);
        if (response.isCallSuccess() && response.isBusinessSuccess()) {
            UpdateBankCardMobileNoRequestDTO requestDTO = new UpdateBankCardMobileNoRequestDTO();
            requestDTO.setAccountNo(response.getPlatformUserNo());
            requestDTO.setBankCardNo(response.getBankcardNo());
            requestDTO.setBankCardMobileNo(response.getMobile());
            logger.info("修改银行卡预留手机号回调处理###修改请求###request={}", JSON.toJSONString(requestDTO));
            boolean isUpdateSuccess;
            try {
                isUpdateSuccess = bankCardDubboService.updateBankCardMobileNo(requestDTO);
                logger.info("修改银行卡预留手机号回调处理###修改响应###response={}", isUpdateSuccess);
            } catch (BizException e) {
                logger.error("修改银行卡预留手机号回调处理###修改异常###errorCode={}, errorMessage={}", e.getErrorCode(), e.getMessage());
                throw e;
            } catch (Exception e) {
                logger.error(String.format("修改银行卡预留手机号回调处理###未知异常###errorMessage=%s", e.getMessage()), e);
                throw new BizException("SYS9999");
            }
            HandleResult result = new HandleResult();
            result.setNeedAck(true);
            result.setData(Collections.singletonMap("orderStatus", isUpdateSuccess ? "SUCCESS" : "FAIL"));
            result.setRequestNo(response.getRequestNo());
            return result;
        }
        throw new BizException(response.getErrorCode(), response.getErrorMessage());
    }


    @Setter
    @Getter
    private static class BankCardMobileNoGatewayResponse {

        private static final String CALL_SUCCESS_CODE = "0";
        private static final String BUSINESS_SUCCESS_CODE = "SUCCESS";

        // 调用状态(0 为调用成功、1 为失败,返回 1 时请看【调用失败错误码】及错误码)
        private String code;

        // 业务处理状态(处理失败 INIT;处理成功 SUCCESS),平台可根据非 SUCCESS 状态做相应处理,处理失败时可参考错误码
        private String status;
        // 错误码
        private String errorCode;
        // 错误码
        private String errorMessage;
        //平台用户编号
        private String platformUserNo;
        //银行卡号
        private String bankcardNo;
        // 银行编码
        private BankCodeEnum bankcode;

        // 请求流水号
        private String requestNo;

        // 集团账户编号
        private String groupAccountNo;

        // 更新后用户手机号
        private String mobile;

        private boolean isCallSuccess() {
            return StringUtils.isNotBlank(code) && CALL_SUCCESS_CODE.equals(code);
        }

        public boolean isBusinessSuccess() {
            return StringUtils.isNotBlank(status) && BUSINESS_SUCCESS_CODE.equals(status);
        }

    }

}
